import { Injectable } from '@nestjs/common';
import { Permission } from '../permission/entities/permission.entity';

@Injectable()
export class ShareService {
  /** 生成权限树 */
  genPermissionTree(data: Permission[], parentId: number | null) {
    const result: Permission[] = [];

    for (const permission of data) {
      if (permission.parentId === parentId) {
        const children = this.genPermissionTree(data, permission.id);
        if (children.length) {
          permission.children = children;
        }
        result.push(permission);
      }
    }

    return result;
  }
}
